package com.example.educationsystem.service.Impl;


import com.example.educationsystem.dao.UserMapper;
import com.example.educationsystem.pojo.User;
import com.example.educationsystem.service.UserService;
import com.example.educationsystem.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> getUSers() {
        return userMapper.getUser();
    }
    @Override
    public ResultVO CheckLogin(LoginVo loginVo) {

        log.info("用户登录UserService接收参数：{}", loginVo);
        log.info("用户登录UserService向UserMapper传入参数：{}", loginVo.getUsername());
        User result = userMapper.selectUserByUsername(loginVo.getUsername());
        log.info("用户登录UserMapper返回参数：{}", result);
        if (result != null) {

            if (MD5Utils.md5(loginVo.getPassword()).equals(result.getUserPwd())) {
                //生成一个token
                String token = JwtUtils.createToken(result.getUserName(), UUID.randomUUID().toString());

                //账号密码正确
                return new ResultVO(ResStatus.OK, token, result);
            } else {
                //账号密码错误
                return new ResultVO(ResStatus.NO, ResStatus.LOGIN_MSG_FAIL, null);
            }

        }
        return new ResultVO(ResStatus.NO, ResStatus.UNKNOWN_ERROR, null);
    }
    @Override
    public ResultVO login(String username, String password) {
        User users = userMapper.selectUserByUsername(username);
        if (users == null) {
            return new ResultVO(10001, "用户名不存在", null);
        } else {
            String md5 = MD5Utils.md5(password);
            String password1 = users.getUserPwd();
            if (md5.equals(password1)) {
                return new ResultVO(10000, "登陆成功", users);
            } else {

                return new ResultVO(10001, "密码错误", null);
            }


        }
    }
    @Override
    public List<User> getUserByUserFamily(Integer userFamily) {
        return userMapper.getUserByUserFamily(userFamily);
    }

    @Override
    public int updateuser(Integer userId, String userCall, String userName, Integer userAge) {
        return userMapper.updateuser(userId,userCall,userName,userAge);
    }

    @Override
    public int deleteuser(Integer userId) {
        return userMapper.deleteuser(userId);
    }


}

